// pages/user/user.js
let utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    host: utils.default.host,
    imgHost: utils.default.imgHost
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this
    self.getBgImg() // 获取背景图
    self.getCustomerProd() // 获取用户已购买符的接口
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let self = this
    self.getCustomerProd() // 获取用户已购买符的接口
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  // 获取背景图
  getBgImg: function () {
    let self = this
    wx.request({
      url: utils.default.host + '/api/decorate/getDecorateList.json?versionId=' + utils.default.versionId + '&code=BACKGROUND_IMAGE',
      success: function (res) {
        if (res.data.success) {
          self.setData({
            bgImg: utils.default.imgHost + res.data.result[0].decorateImageVoList[0].imageUrl
          })
        }
      }
    })
  },
  // 获取用户已购买符
  getCustomerProd: function () {
    let self = this
    let thisApp = getApp()
    wx.request({
      url: utils.default.host + '/api/customer/getCustomerProductList.json',
      method: 'GET',
      data: {
        versionId: utils.default.versionId,
        customerId: thisApp.globalData.userInfo.openId
        // customerId: 'oYdjb4tT-_iVvrL5DTwrM0bfpl0g'
        // customerId: 'oYdjb4i2Z9SWPC_C-ijqIWFKe0oo'
        // customerId: 'oYdjb4kyAn_uPTdZMzksfJ9V7Gf0'
        // customerId: 'oYdjb4gKyVde38vVqL25T_yjaUGA'
        // customerId: 'oYdjb4hHMbNjgqAV8JEt5pUlfHFE'
        // customerId: 'oYdjb4nvAZWljDrFSjREi3nMao7Y'
      },
      success: function (res) {
        if (res.data.success) {
          // console.log('123',res.data)
          var prodArr = [];
          for (var i = 0; i < res.data.result.length;i++){
            res.data.result[i].createDate = self.formatTime(res.data.result[i].createDate, 'Y-M-D')
            prodArr.push(res.data.result[i])
          }
          self.setData({
            customerProd: prodArr
          })
        }
      }
    })
  },
  // 跳转到详情页
  toDetail: function (e) {
    let pdid = e.currentTarget.dataset.pdid
    let orderid = e.currentTarget.dataset.orderid
    let status = e.currentTarget.dataset.status
    wx.navigateTo({
      url: '/pages/detail/detail?orderCode=' + orderid + '&isBuy=1&productId=' + pdid + '&type=user',
    })
  },
  toHomepage: function () {
    wx.switchTab({
      url: '../homepage/homepage'
    });
  },
  formatNumber: function (n) {
    n = n.toString();
    return n[1] ? n : '0' + n;
  },
  formatTime: function (number, format) {
    let self = this
    var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
    var returnArr = [];
    var date = new Date(number);
    returnArr.push(date.getFullYear());
    returnArr.push(self.formatNumber(date.getMonth() + 1));
    returnArr.push(self.formatNumber(date.getDate()));
    returnArr.push(self.formatNumber(date.getHours()));
    returnArr.push(self.formatNumber(date.getMinutes()));
    returnArr.push(self.formatNumber(date.getSeconds()));
    for (var i in returnArr) {
      format = format.replace(formateArr[i], returnArr[i]);
    }
    return format;
  }
})